package com.addcn.android.design591.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.addcn.android.design591.R;
import com.addcn.android.design591.adapter.DesignImageAdapter;
import com.addcn.android.design591.bean.DesignImageBean;
import com.addcn.android.design591.comm.Configs;
import com.addcn.android.design591.comm.OkHttpConn;
import com.addcn.android.design591.utils.L;
import com.addcn.android.design591.utils.SnackbarUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by xuge on 2017/1/19.
 * 图库设计
 */

public class DesignImageActivity extends AppCompatActivity {
    private Context context = this;
    private Toolbar toolbar;
    private AppBarLayout appbarlayout;
    private CoordinatorLayout coordinatorlayout;
    private NavigationView navigationview;
    private DrawerLayout drawerlayout;
    private RecyclerView recyclerview;
    private SwipeRefreshLayout swiperefreshlayout;
    private DesignImageBean designImageBean;
    private ArrayList<DesignImageBean.DataBean.WorksListBean> arrayList = new ArrayList<>();
    private DesignImageAdapter designImageAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.designimage);
        initView();

        configView();

        getData();
    }

    private void getData() {
        String url = Configs.HOST + Configs.URL_DESIGNIMAGE + "offset=" + 1 + "&list_rows=10";
        OkHttpConn.getOkhttp(context, url, new OkHttpConn.OkHttpCallBack() {
            @Override
            public void success(Call call, Response response) throws IOException {
                final String value = response.body().string();
                L.i(value);
                configRecyclerView(value);
            }

            @Override
            public void fail(Call call, IOException e) {

            }
        });
    }

    private void configRecyclerView(final String value) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (value.isEmpty())
                    return;
                designImageBean = new Gson().fromJson(value, DesignImageBean.class);
                if (designImageBean != null) {
                    arrayList.clear();
                    arrayList.addAll(designImageBean.data.works_list);
                    designImageAdapter.notifyDataSetChanged();
                }
            }
        });
    }


    private void configView() {
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SnackbarUtil.show(view, "test", 0);
            }
        });
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        appbarlayout = (AppBarLayout) findViewById(R.id.appbarlayout);
        coordinatorlayout = (CoordinatorLayout) findViewById(R.id.coordinatorlayout);
        navigationview = (NavigationView) findViewById(R.id.navigationview);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);

        swiperefreshlayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout);

        layoutManager = new GridLayoutManager(context,2,GridLayoutManager.VERTICAL,false);
//        layoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
//        layoutManager =  new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        designImageAdapter = new DesignImageAdapter(context, arrayList);
        designImageAdapter.setOnItemClickListener(new DesignImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }
        });
        recyclerview.setAdapter(designImageAdapter);

        swiperefreshlayout.setOnRefreshListener(onRefreshListener);

        recyclerview.setLayoutManager(layoutManager);
    }


    /**
     * 点击事件
     */
    DesignImageAdapter.OnItemClickListener onItemClickListener = new DesignImageAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {

        }
    };

    /**
     * 下拉刷新
     */
    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    swiperefreshlayout.setRefreshing(false);
                }
            }, 2000);
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.design_toolbar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (!drawerlayout.isDrawerOpen(Gravity.RIGHT)) {
            drawerlayout.openDrawer(Gravity.RIGHT);
        }
        return super.onOptionsItemSelected(item);
    }
}
